/**
 * Copyright (C) 2010 Cloudfarming <info@cloudfarming.nl>
 *
 * Licensed under the Eclipse Public License - v 1.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.eclipse.org/legal/epl-v10.html
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package nl.cloudfarming.client.crop;

import java.util.List;
import java.util.Locale;
import org.openide.util.Lookup;

/**
 * Provides crop data.
 * This will be from a localized module.
 *
 *
 * @author Timon Veenstra
 */
public interface CropListProvider {

    /**
     * get the module name for this provider
     * must be unique, for example "crop-nl" for nl localized crop list
     *
     * @return
     */
    String getModuleName();

    /**
     * retrieve a crop based on its key
     * Will return null if no crop can be found for specified key
     *
     * @param Key
     * @return
     */
    Crop getCropForKey(String key);

    /**
     * Retrieve a list with all available crops
     *
     * @return
     */
    List<Crop> getCrops();

    /**
     * Retrieve a list with all valid classifications
     * 
     * @return
     */
    List<Classification> getClassifications();

    /**
     * Locator class can be used to locate a CropListProvider implementation
     *
     */
    public static final class Locator {

        /**
         * guess module name based on default locale
         *
         * @return
         */
        private static String guessModuleName() {
            return getModuleNameFromLocale(Locale.getDefault());
        }

        /**
         * construct module name based on a locale
         *
         * @param locale
         * @return
         */
        private static String getModuleNameFromLocale(Locale locale) {
            return "crop-" + locale.getCountry().toLowerCase();
        }

        /**
         * get the default provider, default locate will be used to determine desired module name
         *
         * @return
         */
        public static CropListProvider findProvider() {
            return findProvider(guessModuleName());
        }

        /**
         * get the provider based on locale
         *
         * @param locale
         * @return
         */
        public static CropListProvider findProvider(Locale locale) {
            return findProvider(getModuleNameFromLocale(locale));
        }

        /**
         * get the provider for a specific module name
         *
         *
         * @param moduleName name of the CropListProvider module
         * @return
         */
        public static CropListProvider findProvider(String moduleName) {
            for (CropListProvider provider : Lookup.getDefault().lookupAll(CropListProvider.class)) {
                if (moduleName.equals(provider.getModuleName())) {
                    return provider;
                }
            }
            CropListProvider provider = Lookup.getDefault().lookup(CropListProvider.class);
            if (provider == null) {
                throw new NoProviderFoundException("Unable to find cropListProvider for module " + moduleName);
            }
            return provider;
        }
    }
}
